<?php namespace WoXuanWang\Settings\Repositories;

use WoXuanWang\Settings\Models\Setting;
use WoXuanWang\Upload\Models\Upload;
use WoXuanWang\User\Repositories\RoleRepository;
use zgldh\Scaffold\BaseRepository;

class SettingsRepository extends BaseRepository
{
    const DASHBOARD_GRIDS = 'dashboard-grids';// Dashboard 配置

    private $cache = [];

    /**
     * 设置配置项
     * @param $key        名字
     * @param null $value 值
     * @return boolean true 设置成功|false 设置失败
     */
    public function set($key, $value = null)
    {
        if (!is_array($key)) {
            $key = [
                $key => $value
            ];
        }
        foreach ($key as $k => $value) {
            $setting = Setting::where('field', $k)->first();
            if (!$setting) {
                $setting = new Setting();
                $setting->field = $k;
            }
            $setting->data = $value;
            $settingSetResult = $setting->save();
            if ($settingSetResult) {
                $this->cache[$k] = $value;
                return true;
            }
        }
        return false;
    }

    /**
     * 获取配置项
     * @param null $key     名字
     * @param null $default 默认值
     * @return null
     */
    public function get($key = null, $default = null)
    {
        if (isset($this->cache[$key])) {
            return $this->cache[$key];
        }
        $setting = Setting::where('field', $key)->first();
        if ($setting) {
            $this->cache[$key] = $setting->data;
            return $setting->data;
        }
        return $default;
    }

    /**
     * Specify Model class name
     *
     * @return string
     */
    public function model()
    {
        return Setting::class;
    }

    public function getDashboardSettingKey($user = null)
    {
        $user = $user ?: \Auth::user();
        if ($user->hasRole(RoleRepository::ROLE_ADMIN)) {
            return self::DASHBOARD_GRIDS;
        } elseif ($user->hasRole(RoleRepository::ROLE_CUSTOMER)) {
            return SettingsRepository::DASHBOARD_GRIDS . '-customer-' . $user->customer->id;
        } elseif ($user->hasRole(RoleRepository::ROLE_SUPERVISOR)) {
            return SettingsRepository::DASHBOARD_GRIDS . '-supervisor-' . $user->supervisor->id;
        }
        return self::DASHBOARD_GRIDS;
    }

    /**
     * 储存 Dashboard 上一个 Grid 的配置内容
     * @param $gridId [1,9]
     * @param $content
     * @return bool
     */
    public function setDashboardGrid($gridId, $content, $key = self::DASHBOARD_GRIDS)
    {
        $grids = $this->getDashboardGrids($key);
        $grids[$gridId] = $content;
        return $this->set($key, json_encode($grids));
    }

    /**
     * 得到所有的 Dashboard 配置内容
     * @param string $key
     * @return null
     */
    public function getDashboardGrids($key = self::DASHBOARD_GRIDS)
    {
        $grids = json_decode($this->get($key), true);
        foreach ($grids as $index => $grid) {
            if (isset($grid['type']) && $grid['type'] == 'upload') {

                if (is_numeric($grid['upload'])) {
                    $grid['upload'] = Upload::find($grid['upload']);
                } elseif (is_numeric($grid['upload']['id'])) {
                    $grid['upload'] = Upload::find($grid['upload']['id']);
                }

                $grids[$index] = $grid;
            }
        }
        return $grids;
    }
}